package search.kspatial.cn.servlet;

import search.kspatial.cn.pojo.Element;
import search.kspatial.cn.pojo.Images;
import search.kspatial.cn.pojo.User;
import search.kspatial.cn.service.ElementService;
import search.kspatial.cn.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author KK
 * @create 2021/11/14 21:41
 */
@WebServlet(name = "/home", urlPatterns = "/home")
public class HomeHandler extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ElementService elementService = new ElementService();
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("gbk");
        String method = req.getParameter("method");
        switch (method) {
            case "login":
                login(req, resp, new UserService());
                break;
            case "add":
                add(req, resp, elementService);
                break;
            case "list":
                list(req, resp, elementService);
                break;
        }
    }


    protected void login(HttpServletRequest req, HttpServletResponse resp, UserService userService) throws IOException, ServletException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        PrintWriter writer = resp.getWriter();
        System.out.println(password);
        User user = userService.login(new User(username, password));
        if (user != null) {
            req.getSession().setAttribute("user", user);
            req.getRequestDispatcher("home.jsp").forward(req, resp);
        } else {
            writer.write("<script language='javascript'>alert('账号或密码错误!');history.go(-1)</script>");
        }
    }

    protected void add(HttpServletRequest req, HttpServletResponse resp, ElementService elementService) throws IOException {
        PrintWriter writer = resp.getWriter();
        String title = req.getParameter("name");
        User user = (User) req.getSession().getAttribute("user");
        String eid = title + user.getUsername();
        if (elementService.findElement(eid) == null) {
            String href = req.getParameter("path");
            int id = Integer.parseInt(req.getParameter("id"));
            Element element = new Element(href, title, id, eid);
            elementService.addElement(element, user);
            writer.write("<script language='javascript'>alert('添加成功!');history.go(-1)</script>");
        } else {
            writer.write("<script language='javascript'>alert('标签名已存在请更换!');history.go(-1)</script>");
        }
    }

    protected void list(HttpServletRequest req, HttpServletResponse resp, ElementService elementService) throws IOException {
        List<Images> all = elementService.findALLImages();
        HttpSession session = req.getSession();
        session.setAttribute("imagesList", all);
        session.setAttribute("imagesCount", 1);
        resp.sendRedirect("home.jsp");
    }
}
